package com.xy.modular.sys.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.sys.entity.role.Role;
import com.xy.modular.sys.entity.role.RoleMenu;
import com.xy.modular.sys.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
	static Logger log = LoggerFactory.getLogger(RoleController.class);

	@Resource
	private IRoleService roleService;

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject list(HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, roleService.findAll(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/tree/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject treeList(HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, roleService.findAll(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(Role role) {
		try {
			roleService.validateEntity(role);
			role.setId(UUIDUtils.UUID());
			roleService.save(role);
		} catch (Exception e) {
			log.info("角色保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, role);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(Role role) {
		try {
			roleService.validateEntity(role);
			roleService.update(role);
		} catch (Exception e) {
			log.info("角色保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, role);
	}

	/**
	 * 分配权限
	 * @param role
	 * @param roleMenu
	 * @return
	 */
	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/menu/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject roleMenuUpdate(Role role, RoleMenu roleMenu) {
		try {
			roleService.roleMenuUpdate(roleMenu);
		} catch (Exception e) {
			log.info("权限分配失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, role);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(Role role, @PathVariable String id) {
		try {
			if (id.equals("SYSTEMER")) {
				return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
			}
			roleService.deleteById(id);
		} catch (Exception e) {
			log.info("角色删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
